package com.cjc.express.serviceImpl;


import com.cjc.express.entity.*;
import com.cjc.express.exception.ValidationException;
import com.cjc.express.mapper.CompanyMapper;
import com.cjc.express.mapper.PriceMapper;
import com.cjc.express.mapper.TimesMapper;
import com.cjc.express.service.UserService;
import com.cjc.express.utils.ExpressUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ====================
 * Created with intellij IDEA
 * 创建者：杜一平
 * 日期：2017/1/19（星期五）
 * 时间：08：00
 * 说明：该类重写用户服务层接口的方法，用于具体实现用户相关操作
 */

@Service(value = "userService")
public class UserServiceImpl implements UserService {
    // 自动装配报价表映射层
    @Autowired
    private PriceMapper priceMapper;

    // 自动装配公司表映射层
    @Autowired
    private CompanyMapper companyMapper;

    // 自动装配用户使用记录次数
    @Autowired
    private TimesMapper timesMapper;

    /**
     * 查询所有公司
     * @return List<Company> 公司集合
     */
    @Override
    public List<Company> listallcompany() {
        try {
            List<Company> companies = companyMapper.selectAllCompany();
            if (companies.isEmpty()) {
                throw new ValidationException("暂无相关记录，工作人员正在赶来的路上!");
            }
            return companies;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 依据收件地查询价格表
     * @param dest 收件地
     * @return List<Price> 报价表集合
     */
    @Override
    public List<Price> selectByDest(String dest) {
        try {
            // 判断输入是否合法
            if (dest == null || dest.trim().isEmpty() || dest.length() > 20) {
                throw new ValidationException("亲，您的输入有误，请重新输入!");
            }
            // 获取相关记录
            List<Price>pricestemp=priceMapper.selectByDest(dest);
            List<Price>prices=ExpressUtils.getPriceDest(pricestemp);
            if (null != prices && !prices.isEmpty()) {
                // 成功
                return prices;
            } else {
                // 无数据情况
                throw new ValidationException("暂无相关记录，工作人员正在赶来的路上!");
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 依据公司查询价格表
     * @param companyname 公司名字
     * @return List<Price> 报价表集合
     */
    @Override
    public List<Price> selectByCompany(String companyname) {
        try {
            // 判断字段是否合法
            if (companyname == null || companyname.length() <= 0 || companyname.length() > 8)
                throw new ValidationException("亲，您的输入有误，请重新输入!");
            // 获取查询数据
            List<Price>pricestemp=priceMapper.selectByCompany(companyname);
            List<Price> prices = ExpressUtils.getPriceCompany(pricestemp);

            if (prices.isEmpty()) {
                // 无数据情况
                throw new ValidationException("暂无相关记录，工作人员正在赶来的路上!");
            } else {
                // 查询成功
                return prices;
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 查询所有价格表
     * @param pageNum
     * @param pageSize
     * @return Page<Price> 可分页报价表
     */
    @Override
    public Page<Price> selectAllPrice(Integer pageNum, Integer pageSize) {
        try {

            if (null == pageNum || null == pageSize || pageNum.toString().length() > 3
                    || pageSize.toString().length() > 3
                    || pageNum == 0 || pageSize == 0) {
                pageNum = 1;
                pageSize = 10;
            }
            PageHelper.startPage(pageNum, pageSize);
            Page<Price> prices = priceMapper.selectAllPrice();
            if (prices.isEmpty()) {
                throw new ValidationException("暂无相关记录，工作人员正在赶来的路上!");
            }
            return prices;
        } catch (Exception e) {
            throw e;
        }

    }

    /**
     * 记录用户使用次数
     * @param times
     * @return 0 失败 1 成功
     */
    @Override
    public int updatePlus(Times times) {
        try {
            if (ExpressUtils.JudgeNull(times.getId())) {
                throw new ValidationException("信息有勿，id必须填写");
            }
            Times times1 = timesMapper.selectByPrimaryKey(times.getId());
            if (null == times1) {
                times.setTime(0);
                if (1 != timesMapper.insertSelective(times))
                    throw new ValidationException("操作失败");
            }
            if (1 != timesMapper.updatePlus(times))
                throw new ValidationException("操作失败");
            return 1;
        } catch (Exception e) {
            throw e;
        }
    }
}
